// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');

// const DEV_MODE = process.env.NODE_ENV === 'development';
const PRO_MODE = process.env.NODE_ENV === 'production';

function resolve(dir) {
    return path.join(__dirname, dir);
}
module.exports = {
    css: {
        loaderOptions: {
            sass: {
                prependData: "@import '~@/styles/mixins', '~@/styles/variables'"
            },
            scss: {
                prependData: "@import '~@/styles/mixins', '~@/styles/variables';"
            }
        }
    },
    assetsDir: 'static',
    productionSourceMap: false,
    publicPath: PRO_MODE ? '' : '/',
    configureWebpack: {
        devtool: PRO_MODE ? '' : 'source-map',
        externals: { AMap: "AMap" }
    },
    chainWebpack: (config) => {
        config.resolve.alias
            .set('@core', resolve('src/components'))
            .set('services', resolve('src/services'));
        config.plugin("prefetch").tap(options => {
            options[0].fileBlacklist = options[0].fileBlacklist || [];
            options[0].fileBlacklist.push(/myasyncRoute(.)+?\.js$/);
            return options;
        });
        config.plugin("html").tap(options => {
            options[0].title = "乡村资源管理交易平台";
            return options;
        });
        const svgRule = config.module.rule("svg");
        const iconSrc = "src/icons";
        // 清除已有的所有 loader。
        // 如果你不这样做，接下来的 loader 会附加在该规则现有的 loader 之后。
        svgRule.uses.clear();
        svgRule
            .test(/\.svg$/)
            .include.add(resolve(iconSrc))
            .end()
            .use("svg-sprite-loader")
            .loader("svg-sprite-loader")
            .options({
                symbolId: "icon-[name]"
            });
        const fileRule = config.module.rule("svgNotSprite");
        fileRule.uses.clear();
        fileRule
            .test(/\.svg$/)
            .exclude.add(resolve(iconSrc))
            .end()
            .use("file-loader")
            .loader("file-loader")
            .options({
                name: "static/img/[name].[hash:8].[ext]"
            });
    },
    devServer: {
        // after: require('./mock/server.js')
    }
};
